public class Solution {

    public ListNode detectCycle(ListNode head) {

        ListNode fast = head;

        ListNode slow = head;

        while (fast != null && fast.next != null) {

            fast = fast.next.next;

            slow = slow.next;

            if (slow == fast) break;

        }

        if (fast != null && fast.next != null) {

            ListNode cur = head;

            while (cur != slow) {

                slow = slow.next;

                cur = cur.next;

            }

            return cur;

        } else return null;


    }
}